﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AvatarHelper;
using Avatar.Model;

public partial class Management_ArticleManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }
    protected void btn_Edit_Click(object sender, EventArgs e)
    {
        try
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                Button _btn = sender as Button;
                var _id = new Guid(_btn.CommandArgument);
                var _news = _adc.News.FirstOrDefault(n => n.ID == _id);
                if (_news != null)
                {
                    ddl_NewsCategory2.SelectedValue = _news.CategoryID.ToString();
                    txt_Title.Text = _news.Title;
                    txt_Content.Value = _news.Content;
                    txt_ID.Value = _news.ID.ToString();
                }
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "" + _e.Message;
        }
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                Button _btn = sender as Button;
                var _id = new Guid(_btn.CommandArgument);
                var _news = _adc.News.FirstOrDefault(n => n.ID == _id);
                if (_news != null)
                {
                    _adc.News.DeleteOnSubmit(_news);
                    _adc.SubmitChanges();
                    GridView1.DataBind();
                }
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "" + _e.Message;
        }
    }
    protected void ddl_Category_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            if (txt_Content.Value.Length > 4000) throw new Exception("内容不能大于4000个字符");
            if (txt_Title.Text.Length > 100) throw new Exception("内容不能大于100个字符");
            if (string.IsNullOrEmpty(txt_Content.Value)) throw new Exception("内容不能为空");
            if (string.IsNullOrEmpty(txt_Title.Text)) throw new Exception("内容不能为空");
            if (string.IsNullOrEmpty(txt_ID.Value)) throw new Exception("新闻ID不能为空");

            using (var _adc = DBHelper.GetDataContext())
            {
                var _id=new Guid(txt_ID.Value);
                News _n = _adc.News.FirstOrDefault(n=>n.ID==_id);
                _n.Title = txt_Title.Text;
                _n.Content = txt_Content.Value;
                _n.CategoryID = Convert.ToInt32(ddl_NewsCategory2.SelectedValue);

                _adc.SubmitChanges();

                txt_Title.Text = "";
                txt_Content.Value = "";
                ddl_NewsCategory2.Dispose();
                GridView1.DataBind();
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "添加新闻出错：" + _e.Message;
        }
    }
}
